<?php
namespace xcom\model;
use x\Xbase;

class StepToken extends Xbase {
	/**
	 * 创建token
	 * @return [type] [description]
	 */
	public function create($ext = '') {
		$this->db->query('delete from step_token where create_time<?', [time() - 30 * 60]);
		$token = token();
		$d = [
			'token' => $token,
			'ip' => ip(),
			'ext' => $ext,
			'end_time' => time() + 5 * 60,
			'create_time' => time(),
		];
		$this->db->insert('step_token', $d);
		return $token;
	}

	/**
	 * 验证上一步下一步token
	 * @param  [type] $step_token [token]
	 * @return [type]        [description]
	 */
	public function checkToken($step_token, $ext = '') {
		$rs = $this->db->query('select * from step_token where token=? and ext=?', [$step_token, $ext], 1);
		if ($rs) {
			$this->db->delete('step_token', $rs['id']);
			if ($rs['end_time'] < time()) {
				$this->setError('验证token过期,请重新回到上一步');
				return false;
			}
			return true;
		}
		$this->setError('上一步验证token无效');
		return false;
	}
}
